package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.News;
import vn.edu.ptithcm.pim.query.NewsQuery;

@SuppressWarnings("rawtypes")
public class NewsDao extends HibernateDaoSupport {

	public void save(News news) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(news);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(News news) {
		try {
			Transaction tx = getSession().beginTransaction();
			if (news.getNewsid() != null) {
				getHibernateTemplate().merge(news);
			} else {
				getHibernateTemplate().saveOrUpdate(news);
			}
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(News news) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(news, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(News News) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(News);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public News merge(News news) {
		try {
			return (News) getHibernateTemplate().merge(news);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public News findById(java.lang.Long id) {
		try {
			return (News) getHibernateTemplate().get(News.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(News news) {
		try {
			return getHibernateTemplate().findByExample("News", news);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(News.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<News> getNewsByType(String type, int max, int offset) {
		try {
			Criteria c = getSession().createCriteria(News.class);
			c.add(Restrictions.ilike("type", "%" + type + ""));
			c.setFirstResult(offset);
			c.setMaxResults(max);
			c.addOrder(Order.asc("updatedAt"));
			return c.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<News> getNewNewsByType(String type) {
		try {
			StringBuilder sql = new StringBuilder();
			sql.append(
					"from News as news where news.type like '%" + type + "%'")
					.append(" and day(current_date()) - day(news.updatedAt) <= 7 ")
					.append(" order by news.updatedAt desc");
			getHibernateTemplate().setMaxResults(8);
			return getHibernateTemplate().find(sql.toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void deleteById(java.lang.Long id) {
		try {
			Transaction tx = getSession().beginTransaction();
			StringBuilder sql = new StringBuilder();
			sql.append("delete News as news where news.newsid ='").append(id)
					.append("'");
			getHibernateTemplate().bulkUpdate(sql.toString());
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<News> findByQuery(NewsQuery query, int max, int offset) {
		try {
			String searchKey = StringUtils.trimToEmpty(query.getSearchKey());
			if ("Keyword...".equals(searchKey)) {
				searchKey = "";
			}
			Criteria c = getSession().createCriteria(News.class);
			if (!searchKey.isEmpty()) {
				c.add(Restrictions.ilike("title", "%" + searchKey + "%"));
			}
			c.addOrder(Order.desc("updatedAt"));
			return c.setFirstResult(offset).setMaxResults(max).list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public int countByQuery(NewsQuery query) {
		try {
			String searchKey = StringUtils.trimToEmpty(query.getSearchKey());
			if ("Keyword...".equals(searchKey)) {
				searchKey = "";
			}
			Criteria c = getSession().createCriteria(News.class);
			if (!searchKey.isEmpty()) {
				c.add(Restrictions.ilike("title", "%" + searchKey + "%"));
			}
			c.setProjection(Projections.rowCount());
			return Integer.parseInt(c.list().get(0).toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
